package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.scity.beens.HdglplListBeen;
import com.example.scity.beens.ShjfBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HdglPlActivity extends AppCompatActivity {
    ListView hdglpllistView;
    HdglplListBeen hdglplListBeen;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    HdglplListView();
                    break;
            }
        }
    };
    private void HdglplListView() {
        hdglpllistView=findViewById(R.id.hdglpllistview);
        hdglpllistView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return hdglplListBeen.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return hdglplListBeen.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = LayoutInflater.from(HdglPlActivity.this).inflate(R.layout.item_hdglpllist, null);
                TextView userName;
                userName = view.findViewById(R.id.item_hdglpl_userName);
                userName.setText("用户名:"+hdglplListBeen.rows.get(i).commentTime);TextView activityName;
                activityName = view.findViewById(R.id.item_hdglpl_hdname);
                activityName.setText("活动名:"+hdglplListBeen.rows.get(i).activityName);
                TextView nicheng;
                nicheng = view.findViewById(R.id.item_hdglpl_nicheng);
                nicheng.setText("昵称:"+hdglplListBeen.rows.get(i).nickName);
                TextView content;
                content = view.findViewById(R.id.item_hdglpl_content);
                content.setText(Html.fromHtml(hdglplListBeen.rows.get(i).content));
                TextView pltime;
                pltime = view.findViewById(R.id.item_hdglpl_pltime);
                pltime.setText("评论时间:"+hdglplListBeen.rows.get(i).commentTime);
                return view;
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hdgl_pl);
        LoadHdglpllist();
    }
    private void LoadHdglpllist() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.HdglPlList+"?activityId=1&pageNum=1&pageSize=8")
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                hdglplListBeen = new Gson().fromJson(response.body().string(), HdglplListBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }
}